package com.lanqiao.guimei.controller;

import com.google.gson.Gson;
import com.lanqiao.guimei.dao.CateGoryDao;
import com.lanqiao.guimei.vo.Category;
import com.lanqiao.guimei.vo.Page;

import com.lanqiao.web.mvc.annotation.Controller;
import com.lanqiao.web.mvc.annotation.RequestMapping;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class CategoryController {



    /**
     * 商品分类显示 分页
     * @param request
     * @param session
     * @param category
     * @return
     */
    @RequestMapping("/Category/page")
    public String findForPage(HttpServletRequest request, HttpSession session, Category category){



        Page<Category> categoryPage = (Page<Category>) session.getAttribute("CategoryPage");

        CateGoryDao categoryDao = new CateGoryDao();

        String pageNo = request.getParameter("pageNo");

        if (categoryPage == null || StringUtils.isBlank(pageNo)){
            categoryPage = new Page<Category>();
            categoryPage.setPageSize(3);//每页显示的数
            categoryPage.setPageNo(1);  //当前页数
            categoryPage.setQueryObject(category); //前端传过来的 商品
            // 统计总记录数
            categoryPage.setTotalRow(categoryDao.countForPage(categoryPage));
        } else {
            categoryPage.setPageNo(Integer.valueOf(pageNo));
        }
        session.setAttribute("CategoryPage", categoryPage);

        // 查询列表数据
        List<Category> categoryList = categoryDao.findForPage(categoryPage);
        request.setAttribute("categoryList", categoryList);

        return "/after/prod_type_admin.jsp";
    }


    /**
     * 增加商品分类
     * @param category
     * @return
     */
    @RequestMapping("/Category/AddCategory")
    public Map AddCategory(Category category,HttpServletRequest request) {
        boolean b = new CateGoryDao().AddCategory(category);
        Map json = new HashMap();
        json.put("result", b);
        return json;
    }

    /**
     * 修改商品分类
     * @param category
     * @param request
     * @return
     */
    @RequestMapping("/Category/ModifyCategory")
    public Map ModifyCategory(Category category, HttpServletRequest request) {
        boolean b = new CateGoryDao().ModifyCategory(category);
        Map json = new HashMap();
        json.put("result", b); // gson: {result: true, key2: value2, key3: value3}
        return json;
    }


    /**
     * 查出大类big_ctgr
     * @param category
     * @param request
     * @return
     */
    @RequestMapping("/Category/Categories")
    public List<Category> Categories(Category category, HttpServletRequest request) {
        List<Category> categoryList = new CateGoryDao().Categories();
        return categoryList;

    }

    /**
     * 查出小类型通过大类型
     * @param request
     * @return
     */
    @RequestMapping("/Category/queryByBigCtgr")
    public List<Category> queryByBigCtgr(HttpServletRequest request) {
        String bigCtgr = (String)request.getParameter("bigCtgr");
        List<Category> sList = new CateGoryDao().queryByBigCtgr(bigCtgr);
        return sList;

    }
    /**
     * 修改
     * @param request
     * @return
     */
    @RequestMapping("/Category/insert")
    public Boolean insert(HttpServletRequest request) {

        return false;

    }




}
